﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace MagicBag.CRM.Service
{
    public class BaseService : MagicBag.CRM.HttpHandler.IMagicBagAjaxService
    {
        public void ResponseMessage(HttpContext context)
        {
            //判断用户是否登录，凡是没有登录的请求将跳转到登录界面
            /*
            if (string.IsNullOrEmpty(GetUserID()))
                context.Response.Redirect(context.Request.ApplicationPath + "Login.htm");
             */

            string method = context.Request["act"];
            this.GetType().GetMethod(method).Invoke(this, new object[] { context });
        }

        /// <summary>
        /// 返回xml数据，不包含声明
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        public void ResponseXml(HttpContext context, string xmlMsg)
        {
            StringBuilder builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            builder.Append(xmlMsg);
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "text/xml";
            context.Response.Write(builder.ToString());
        }

        /// <summary>
        /// 返回json数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        public void ResponseJson(HttpContext context, string jsonMsg)
        {
           // context.Response.ContentType = "text/plain";//使用纯文本格式，而不是application/json，否则在ie下使用form 提交时将提示文件下载
            context.Response.Write(jsonMsg);
        }

        /// <summary>
        /// 获取Easy UI格式的明细数据的json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public string GetEasyUIDetailJson<T>(IList<T> list,int total) where T:new()
        { 
            string json=JsonHelper.GetJson<IList<T>>(list);
            string json1 = "{\"total\":"+total+",\"rows\":"+json+"}";
            return json1;
        }

        /// <summary>
        /// 获取当前登录用户ID
        /// </summary>
        /// <returns></returns>
        protected string GetUserID()
        {
            if (HttpContext.Current.Session["CurrentUserID"] != null)
                return HttpContext.Current.Session["CurrentUserID"].ToString();
            return string.Empty;
        }
    
    }
}
